﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Manager
{
    public partial class frmCT : Form
    {
        SQL sql = new SQL();
        string strMPB;
        string strNPB;
        string strMCT;
        string strNCT;

        public frmCT()
        {
            InitializeComponent();
        }

        private void frmCT_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            getMCT();
            getMPB(cbMPB);
            getMPB(cbSearchMPB);
           
            cbMPB.Text = "";

            //get value into cbSearchMPB
            cbSearchMPB.DataSource = sql.getDataTable("sql_MPB_cb");
            cbSearchMPB.ValueMember = "MPB";
            cbSearchMPB.Text = "";

            //get value into cbSearchMCT
            cbSearchMCT.DataSource = sql.getDataTable("sql_MCT_cb");
            cbSearchMCT.ValueMember = "MCT";
            cbSearchMCT.Text = "";

            btnDeleteCT.Enabled = false;
            btnEditCT.Enabled = false;
            btnUpdateCT.Enabled = false;
            btnThemCongTrinh.Enabled = false;
        }
        private void getMPB( ComboBox cb)
        {
            cb.DataSource = sql.getDataTable("sql_MPB_cb");
            cb.ValueMember = "MPB";
            cb.Text = "";
        }
        private void getMCT()
        {
            cbSearchMCT.DataSource = sql.getDataTable("sql_MCT_cb");
            cbSearchMCT.ValueMember = "MCT";
            cbSearchMCT.Text = "";
        }
        private void button1_Click(object sender, EventArgs e)
        {
            View();
            btnDeleteCT.Enabled = true;
            btnEditCT.Enabled = true;            
            btnThemCongTrinh.Enabled = true;
        }
        private void View()
        {
            strMPB = cbSearchMPB.Text;
            strNPB = txtSearchNamePB.Text;
            strMCT = cbSearchMCT.Text;
            strNCT = txtSearchNAMECT.Text;
            if ((cbSearchMCT.Text == "") && (cbSearchMPB.Text == "") && (txtSearchNAMECT.Text == "") && txtSearchNamePB.Text == "")
            {
                dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                string query = "sql_view_CongTrinh";
                dataGridView1.DataSource = sql.getDataTable(query);
            }
            else
            {
                dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
                string query = "sql_select_CongTrinh";
                SqlParameter[] sqlPara = new SqlParameter[4];
                sqlPara[0] = new SqlParameter("@MCT", cbSearchMCT.Text.Trim());
                sqlPara[1] = new SqlParameter("@NAMECT", txtSearchNAMECT.Text);
                sqlPara[2] = new SqlParameter("@MPB", cbSearchMPB.Text.Trim());
                sqlPara[3] = new SqlParameter("@NAMEPB", txtSearchNamePB.Text);
                DataTable dt = sql.getDataTable(query, sqlPara);
                dt.Columns[0].ColumnName = "Mã phòng ban";
                dt.Columns[1].ColumnName = "Tên phòng ban";
                dt.Columns[2].ColumnName = "Mã công trình";
                dt.Columns[3].ColumnName = "Tên công trình";
                dt.Columns[4].ColumnName = "Ngày cấp phép";
                dt.Columns[5].ColumnName = "Ngày khởi công";
                dt.Columns[6].ColumnName = "Ngày hoàn thành";
                dt.Columns[7].ColumnName = "Mã công việc";
                dt.Columns[8].ColumnName = "Tên công việc";
                dt.Columns[9].ColumnName = "Price";
                dt.Columns[10].ColumnName = "Dự kiến hoàn thành công việc";
                dataGridView1.DataSource = dt;
            }
            getMCT();
            getMPB(cbMPB);
            getMPB(cbSearchMPB);

            cbSearchMPB.Text = strMPB;
            txtSearchNamePB.Text = strNPB;
            cbSearchMCT.Text = strMCT;
            txtSearchNAMECT.Text = strNCT;
        }

        private void getValue(int index)
        {
            try
            {
                cbMPB.Text = dataGridView1.Rows[index].Cells[0].Value.ToString().Trim();
                tb_ct_npb.Text = dataGridView1.Rows[index].Cells[1].Value.ToString().Trim();
                tb_ct_mct.Text = dataGridView1.Rows[index].Cells[2].Value.ToString().Trim();
                tb_ct_nct.Text = dataGridView1.Rows[index].Cells[3].Value.ToString().Trim();
                dateTimePicker1.Value = Convert.ToDateTime(dataGridView1.Rows[index].Cells[4].Value.ToString().Trim());
                dateTimePicker2.Value = Convert.ToDateTime(dataGridView1.Rows[index].Cells[5].Value.ToString().Trim());
                dateTimePicker3.Value = Convert.ToDateTime(dataGridView1.Rows[index].Cells[6].Value.ToString().Trim());
               
            }
            catch { }
        }

        private void CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int index = e.RowIndex;
            getValue(index);
            readOnly(true);
        }
        private void readOnly(bool f)
        {
            tb_ct_mct.ReadOnly = f;
            tb_ct_nct.ReadOnly = f;
            tb_ct_npb.ReadOnly = f;
            cbMPB.Enabled = !f;
            

        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (tb_ct_mct.Text == "") MessageBox.Show("Hay chon mot cong trinh");
            else
            {
                readOnly(false);
                tb_ct_mct.ReadOnly = true;
                tb_ct_npb.ReadOnly = true;
                btnUpdateCT.Enabled = true;
                btnEditCT.Enabled = false;
                strMPB = cbMPB.Text;
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (strMPB.Equals(cbMPB.Text) == false)
            {
                int index = 0;
                /*
                 * lay MNV tu CT nhan vien cong trinh
                 */
                string strQuery = "sql_MNV_where_MCT";
                SqlParameter sqlPara = new SqlParameter("@MCT", tb_ct_mct.Text);
                DataTable dt = sql.getDataTable(strQuery, sqlPara);

                /*
                 * delete CT_NhanVien_CongTrinh
                 */
                strQuery = "sql_delete_NhanVien_CongTrinh";
                sqlPara = new SqlParameter("@MCT", tb_ct_mct.Text);
                sql.execute_Query(strQuery, sqlPara);

                /*
                 * delete CT_NhanVien_CongViec
                 */               

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    strQuery = "sql_delete_CT_NhanVien_CongViec";
                    sqlPara = new SqlParameter("@MNV", dt.Rows[i][index].ToString());
                    sql.execute_Query(strQuery, sqlPara);
                }

                /*
                 * update Cong trinh
                 */
                strQuery = "sql_update_CongTrinh";
                SqlParameter[] pr = new SqlParameter[6];
                pr[0] = new SqlParameter("@MCT", tb_ct_mct.Text);
                pr[1] = new SqlParameter("@NAMECT", tb_ct_nct.Text);
                pr[2] = new SqlParameter("@MPB", cbMPB.Text);
                pr[3] = new SqlParameter("@DATECP", dateTimePicker1.Value.Date.ToString("MM/dd/yyyy"));
                pr[4] = new SqlParameter("@DATEKC", dateTimePicker2.Value.Date.ToString("MM/dd/yyyy"));
                pr[5] = new SqlParameter("@DATEHT", dateTimePicker3.Value.Date.ToString("MM/dd/yyyy"));
                sql.execute_Query(strQuery, pr);
                MessageBox.Show("Update successful !", "Successful", MessageBoxButtons.OK, MessageBoxIcon.Information);

                btnEditCT.Enabled = true;
                btnUpdateCT.Enabled = false;
                View();
            }
            else
            {
                /*
                * update Cong trinh
                */
                string strQuery = "sql_update_CongTrinh";
                SqlParameter[] pr = new SqlParameter[6];
                pr[0] = new SqlParameter("@MCT", tb_ct_mct.Text);
                pr[1] = new SqlParameter("@NAMECT", tb_ct_nct.Text);
                pr[2] = new SqlParameter("@MPB", cbMPB.Text);
                pr[3] = new SqlParameter("@DATECP", dateTimePicker1.Value.Date.ToString("MM/dd/yyyy"));
                pr[4] = new SqlParameter("@DATEKC", dateTimePicker2.Value.Date.ToString("MM/dd/yyyy"));
                pr[5] = new SqlParameter("@DATEHT", dateTimePicker3.Value.Date.ToString("MM/dd/yyyy"));
                sql.execute_Query(strQuery, pr);
                MessageBox.Show("Update successful !", "Successful", MessageBoxButtons.OK, MessageBoxIcon.Information);

                btnEditCT.Enabled = true;
                btnUpdateCT.Enabled = false;
                View();
            }
        }


        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }


        private void button2_Click(object sender, EventArgs e)
        {   
            AddCT frmAddCT = new AddCT();
            frmAddCT.Show();
            View();
        }

        private void thêmCôngViệcToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (tb_ct_mct.Text == "") MessageBox.Show("Ban chua chon cong trinh nao ");
            else
            {
                AddCV add_cv = new AddCV(tb_ct_mct.Text, tb_ct_nct.Text, tb_ct_npb.Text);
                if (add_cv.ShowDialog() == DialogResult.OK)
                {
                    View();
                }
            }
        }

        private void context_Mousedown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                contextMenu.Show(new Point(Cursor.Position.X, Cursor.Position.Y));
            }
        }

        private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            string query = "sql_NAMEPB_where_MPB";
            dt = sql.getDataTable(query, new SqlParameter("@MPB", cbMPB.Text));
            if (dt.Rows.Count > 0)
            {
                tb_ct_npb.Text = dt.Rows[0][0].ToString();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (tb_ct_mct.Text == "") MessageBox.Show("Hay chon mot cong trinh");
            else
            {
                /*
                 * delete from table CongTrinh
                 */
                string strQuery = "sql_delete_CongTrinh";
                SqlParameter sqlPara = new SqlParameter("@MCT", tb_ct_mct.Text);
                sql.execute_Query(strQuery, sqlPara);

                /*
                 * delete from table CT_NhanVien_CongTrinh
                 */
                strQuery = "sql_delete_NhanVien_CongTrinh";
                sqlPara = new SqlParameter("@MCT", tb_ct_mct.Text);
                sql.execute_Query(strQuery, sqlPara);

                /*
                 * delete from table CT_NhanVien_CongViec
                 */

                strQuery = "sql_MCV_where_MCT";
                sqlPara = new SqlParameter("@MCT", tb_ct_mct.Text);
                DataTable dt = sql.getDataTable(strQuery, sqlPara);

                if (dt != null)
                {
                    strQuery = "sql_delete_CT_NhanVien_CongViec_where_MCV";
                    int index = 0;
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        sqlPara = new SqlParameter("@MCV", dt.Rows[i][index].ToString());
                        sql.execute_Query(strQuery, sqlPara);
                    }
                    MessageBox.Show("Delete successful!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                View();
            }
        }

    }
}
